/*
 * @Author: malgee
 * @Date: 2025-04-17 11:28:15
 * @LastEditors: maling
 * @LastEditTime: 2025-04-25 09:02:38
 * @FilePath: /vue3-ts-cms/src/store/index.ts
 * @Description: 
 * 
 */
import { createStore, useStore as _useStore } from 'vuex'
import login from './login/login'
import system from './main/system/ststem'
import { IStateType } from './types'
import { fetchRoleData, fetchDepartmentData, fetchMenuData } from '@/service/main/system/system'
const store = createStore({
  state: {
    name: '',
    password: '',
    roleData: [],
    departmentData: [],
    menuData: []
  },
  getters: {},
  mutations: {
    setRoleData(state, roleData) {
      state.roleData = roleData
    },
    setDepartmentData(state, departmentData) {
      state.departmentData = departmentData
    },
    setMenuData(state, menuData) {
      state.menuData = menuData
    }
  },
  actions: {

    async getInitialDataAction({ commit }) {
      // 获取角色,部门,菜单数据
      const { data: roleData } = await fetchRoleData({offset: 0, limit: 1000})
      const { data: departmentData } = await fetchDepartmentData({offset: 0, limit: 1000})
      const { data: menuData } = await fetchMenuData({offset: 0, limit: 1000})
      
      commit('setRoleData', roleData)
      commit('setDepartmentData', departmentData)
      commit('setMenuData', menuData)
    }

  },
  modules: {
    login: login,
    system: system
  }
})

// 初始化store
export function setupStore() {
  store.dispatch('login/loadLocalLogin')
}

export function useStore() {
  return _useStore<IStateType>()
}

export default store